/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2015年4月20日         luwei
 */

package com.tianci.appstore.ui.control;

import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.KeyEvent;

import com.coocaa.x.framework.utils.Android;
import com.coocaa.x.framework.utils.LogUtils;
import com.tianci.appstore.SearchActivity;
import com.tianci.appstore.audio.IAudio;
import com.tianci.appstore.ui.data.SkyCategoryData;
import com.tianci.appstore.ui.mainpage.SkyMyAppPage;
import com.tianci.appstore.ui.widget.LeftImageButtonLayer;
import com.tianci.appstore.ui.widget.SkyAppPager;
import com.tianci.appstore.ui.widget.SkyCategoryBar;
import com.tianci.appstore.ui.widget.SkyCustomLayout;
import com.tianci.appstore.ui.widget.SkyGridAdapter;

public abstract class BaseUIController implements IAudio {
    protected SkyAppPager mPager = null;
    protected int mTotalPage = 0;
    protected int mCurrentPage = 0;
    protected Context mContext = null;
    protected int mCurrentPos = 0;
    protected SkyCategoryBar mCategoryBar = null;
    protected List<SkyCategoryData> mCategoryDatas = null;
    protected LeftImageButtonLayer mButtonLayer = null;

    public BaseUIController(Context context) {
        this.mContext = context;
    }

    public void setCategoryBar(SkyCategoryBar categoryBar) {
        this.mCategoryBar = categoryBar;
    }

    public void setButtonLayer(LeftImageButtonLayer layer) {
        this.mButtonLayer = layer;
    }

    public void setCategoryDatas(List<SkyCategoryData> categoryDatas) {
        this.mCategoryDatas = categoryDatas;
    }

    protected HashMap<String, String[]> addCategoryCommand(HashMap<String, String[]> old) {
        if (mCategoryDatas != null && mCategoryDatas.size() > 0) {
            String[] category = new String[mCategoryDatas.size()];
            for (int i = 0; i < mCategoryDatas.size(); i++) {
                old.put(mCategoryDatas.get(i).name, new String[]{mCategoryDatas.get(i).name});
                category[i] = mCategoryDatas.get(i).name;
            }
        }
        return old;
    }

    protected HashMap<String, String[]> addItemCommand(HashMap<String, String[]> old) {
        return ((SkyGridAdapter) getCurrentPage().getAdapter()).getAppListNames(old);
    }

    protected SkyCustomLayout getCurrentPage() {
        if (mPager.getChildAt(mPager.getDisplayedChild()) instanceof SkyCustomLayout) {
            return (SkyCustomLayout) mPager.getChildAt(mPager.getDisplayedChild());
        } else {
            SkyMyAppPage flipper = (SkyMyAppPage) mPager.getChildAt(mPager.getDisplayedChild());
            return flipper.getCurrentChild();
        }
    }

    protected SkyCustomLayout getPage(int index) {
        if (mPager.getChildAt(index) == null) {
            return null;
        }
        if (mPager.getChildAt(index) instanceof SkyCustomLayout) {
            return (SkyCustomLayout) mPager.getChildAt(index);
        } else {
            return ((SkyMyAppPage) mPager.getChildAt(index)).getPageChildAt(0);
        }
    }

    public int getIndexByCategoryData(SkyCategoryData categoryData) {
        int i = 0;
        if (mCategoryDatas == null) {
            return -1;
        }
        for (SkyCategoryData data : mCategoryDatas) {
            LogUtils.d("main category id is " + data.id);
            if (data.id.equals(categoryData.id)) {
                break;
            }
            i++;
        }
        if (i >= mCategoryDatas.size()) {
            i = -1;
        }
        return i;
    }

    public int getIndexByCategoryId(String id) {
        if (mCategoryDatas == null) {
            return -1;
        }
        if (id == null || id.equals("")) {
            if (mCategoryDatas.size() > 2) {
                return 1;
            }
            return 0;
        }
        int i = 0;
        for (SkyCategoryData data : mCategoryDatas) {
            if (data.id.equals(id)) {
                break;
            }
            i++;
        }
        if (i >= mCategoryDatas.size()) {
            i = -1;
        }
        return i;
    }

    public int getIndexByCategoryName(String name) {
        if (mCategoryDatas == null) {
            return -1;
        }
        if (name == null || name.equals("")) {
            if (mCategoryDatas.size() > 2) {
                return 1;
            }
            return 0;
        }
        int i = 0;
        for (SkyCategoryData data : mCategoryDatas) {
            if (data.name.equals(name)) {
                break;
            }
            i++;
        }
        if (i >= mCategoryDatas.size()) {
            i = -1;
        }
        return i;
    }

    public HashMap<String, String[]> getGlobalCommand() {
        HashMap<String, String[]> command = new HashMap<String, String[]>();
        command.put("back", new String[]{"返回"});
        command.put("exit", new String[]{"退出"});
        command.put("search", new String[]{"$P(_SEARCH)"});
        return command;
    }

    @Override
    public boolean exeCommand(Intent intent) {
        boolean exe = false;
        String command = intent.getStringExtra("_command");
        if (command.equals("back")) {
            Android.simulateKey(KeyEvent.KEYCODE_BACK, 0);
            Android.simulateKey(KeyEvent.KEYCODE_BACK, 1500);
            exe = true;
        } else if (command.equals("exit")) {
            Android.simulateKey(KeyEvent.KEYCODE_BACK, 0);
            System.exit(0);
            exe = true;
        } else if (command.equals("search")) {
            Android.simulateKey(KeyEvent.KEYCODE_BACK, 0);
            String key = intent.getStringExtra("keyword");
            LogUtils.d("luwei", "the key is " + key);
            Intent intent2 = new Intent(mContext, SearchActivity.class);
            intent2.putExtra(SearchActivity.SEARCH_KEY, key);
            mContext.startActivity(intent2);
            exe = true;
        }
        return exe;
    }

    public abstract String getScene();
}
